package club.emergency.wechat.question.search.consumer.controller;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.question.search.consumer.service.QuestionSearchConsumer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.question.search.consumer.controller
 * Date: 2018/11/28
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@Api("题目搜索接口")
@RestController
@RequestMapping("/question-search")
public class QuestionSearchController {

    private QuestionSearchConsumer questionSearchConsumer;

    public QuestionSearchController(QuestionSearchConsumer questionSearchConsumer) {
        this.questionSearchConsumer = questionSearchConsumer;
    }

    @ApiOperation("根据id查询题目")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "query")
    @PostMapping("/getById")
    public ResponseEntity getById(
            @RequestParam("id") Long id
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(questionSearchConsumer.getById(id), ResponseEntity.class);
    }

    @ApiOperation("通过题目种类查询题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryAlias", value = "字典表种类别名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "PageSize", value = "页面显示数据数量", dataType = "int", paramType = "query")
    })
    @PostMapping("/flipListByCategoryAlias")
    public ResponseEntity flipListByCategoryAlias(
            @RequestParam(value = "categoryAlias", required = false) String categoryAlias,
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "PageSize", defaultValue = "10") Integer pageSize
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(questionSearchConsumer.flipListByCategoryAlias(categoryAlias, pageNo, pageSize), ResponseEntity.class);
    }

    @ApiOperation("通过题目种类查询题目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "题目id", required = true, dataType = "long", paramType = "query"),
    })
    @PostMapping("/getChoiceOptionByParent")
    public ResponseEntity getChoiceOptionByParent(
            @RequestParam("parentId") Long parentId
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(questionSearchConsumer.getChoiceOptionByParent(parentId), ResponseEntity.class);
    }

    @ApiOperation("随机选题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "size", required = true, dataType = "int", paramType = "query"),
    })
    @PostMapping("/getRandomQuestion")
    public ResponseEntity getRandomQuestion(
            @RequestParam("size") Integer size
    ) {
        return new JsonConvert<ResponseEntity>().jsonDecode(questionSearchConsumer.getRandomQuestion(size), ResponseEntity.class);
    }
}
